<?php
namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\PlatSetting;
use App\Repositories\Contracts\PlatUserRepository;
use App\Repositories\Eloquent\PlatSettingRepository;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;

class AnnounceController extends Controller
{
    protected $key = 'announce';
    protected $settingRepo;

    public function __construct(PlatSettingRepository $repository)
    {
        $this->settingRepo = $repository;
    }

    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('首页公告设置');

            $content->row(function (Row $row) {
                $setting = PlatSetting::find($this->key);
                $data = $setting ? $setting->value :[];
                $form = new  Form();
                $form->textarea($this->key .'.content', '公告内容')->default(dfValueFromArray('content', $data));
                $form->radio($this->key.'.status', '是否显示')->options([
                    0 => '不显示',
                    1 => '显示'
                ])->default((int)dfValueFromArray('status', $data));
                $form->disablePjax();
                $row->column(12, function (Column $column) use ($form) {
                    $column->append($form->render());
                });
            });
        });
    }

    public function store(Request $request)
    {
        $setting = $request->input($this->key, '');
        $plat_setting = PlatSetting::find($this->key);
        if ($plat_setting == null) {
            $plat_setting = new PlatSetting();
            $plat_setting->key = $this->key;
        }
        $plat_setting->value = $setting;
        $plat_setting->save();
        return back();
    }
}
